/*
 * @Author: c
 * @Date: 2024-01-08 22:13:45
 * @LastEditTime: 2024-03-16 17:22:20
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\music\banner\banner.service.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: c
 * @Date: 2024-01-08 22:13:45
 * @LastEditTime: 2024-03-09 23:59:20
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\music\banner\banner.service.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { plainToClass } from 'class-transformer';
import { EXCEPTION } from 'src/common/const/constants';
import { PageResDto } from 'src/common/dto/page.dto';
import { BannerType } from 'src/common/enums/banner.enum';
import { CommonException } from 'src/common/exception/common.exception';
import { User } from 'src/core/system/user/entities/user.entity';
import { DataSource, Repository } from 'typeorm';
import { AlbumService } from '../album/album.service';
import { Pic } from '../album/dto/create-album.dto';
import { AlbumDto } from '../album/dto/search-album.dto';
import { DBanner } from './dao/banner.dao';
import { BannerForm } from './dto/create-banner.dto';
import { BannerPageQuery, BannerVo } from './dto/search-banner.dto';
import { Banner } from './entities/banner.entity';

@Injectable()
export class BannerService {

  constructor(
    @InjectRepository(Banner) private bannerRepository: Repository<Banner>,
    private dataSource: DataSource,
    private dbanner: DBanner,
    private albumService: AlbumService
  ) { }

  /**
   * 轮播图-创建
   * @param bannerForm 
   * @returns 
   */
  async create(bannerForm: BannerForm) {
    const exsitBanner = await this.getOneByOutputId(bannerForm.jumpLinkOutput)
    if (exsitBanner) {
      throw new CommonException(EXCEPTION.COMMON)
    }
    const banner = plainToClass(Banner, bannerForm)
    return this.dbanner.create(banner)
  }

  /**
  * 轮播图-首页
  * @param user 
  * @param param1 
  * @returns 
  */
  async getList({ pageNum, pageSize }: BannerPageQuery) {
    const [list, total] = await this.dbanner.page(pageNum, pageSize)
    const bannerVos = []
    for await (const banner of list) {
      const bannerVO = plainToClass(BannerVo, banner);
      if (banner.jumpType === BannerType.Album) {
        const album: AlbumDto = await this.albumService.getOneById(banner.jumpLinkOutput);
        bannerVO.title = album.title
        bannerVO.pic = (album.pic as Pic).url
        bannerVO.outDate = new Date(banner.showEndDate).getTime() < new Date().getTime()
      }
      bannerVos.push(bannerVO)
    }
    return PageResDto.result(bannerVos, total)
  }

  /**
   * 轮播图-查询
   * @param user 
   * @param param1 
   * @returns 
   */
  async page(user: User, { pageNum, pageSize, bannerName }: BannerPageQuery) {
    const [list, total] = await this.dbanner.page(pageNum, pageSize)
    const bannerVos = []
    for await (const banner of list) {
      const bannerVO = plainToClass(BannerVo, banner);
      if (banner.jumpType === BannerType.Album) {
        const album: AlbumDto = await this.albumService.getAlbumFormData(user, banner.jumpLinkOutput);
        bannerVO.title = album.title
        bannerVO.pic = (album.pic as Pic).url
        bannerVO.outDate = new Date(banner.showEndDate).getTime() < new Date().getTime()
      }
      bannerVos.push(bannerVO)
    }
    return PageResDto.result(bannerVos, total)
  }

  /**
 * 轮播图-获取单条数据
 * @param req 
 * @param id 
 * @returns 
 */
  async findOne(user: User, id: number) {
    const banner: Banner = await this.dbanner.getOneById(id);

    const bannerVO = plainToClass(BannerVo, banner);
    if (banner.jumpType === BannerType.Album) {
      const album: AlbumDto = await this.albumService.getAlbumFormData(user, banner.jumpLinkOutput);
      bannerVO.title = album.title
      bannerVO.pic = (album.pic as Pic).url
      bannerVO.outDate = new Date(banner.showEndDate).getTime() < new Date().getTime()
    }
    return bannerVO
  }
  getOneByOutputId(id: number) {
    return this.dbanner.getOneByOutputId(id)
  }

  async update(user: User, id: number, bannerForm: BannerForm) {
    const banner: Banner = await this.dbanner.getOneById(id);
    if (!banner) {
      throw new CommonException(EXCEPTION.COMMON);
    }
    await this.dbanner.update(banner, bannerForm)
  }

  remove(ids: number | number[]) {
    return this.dbanner.deleteById(ids)
  }
}
